﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Threading;
using System.Threading.Tasks;
using HalalCheck.DataModels;
using HalalCheck.Services;
using Xamarin;
using Xamarin.Forms;
using XLabs.Ioc;
using XLabs.Platform.Device;
using XLabs.Platform.Services;
using XLabs.Platform.Services.Geolocation;

namespace HalalCheck.ViewModels
{
    public class SalahViewModel : BaseViewModel
    {
        private SalatData _salat;
        public SalatData Salat
        {
            get
            {
                return _salat;
            }
            set
            {
                _salat = value;
                _salat.location = _salat.country + " - " + _salat.city + ", muslimsalat.com";// + ": " + _salat.city;
                SalatGenerated = "Fajr - " + _salat.items[0].fajr + "\n\nShurooq - " + _salat.items[0].dhuhr + "\n\nDhuhr - "
                    + _salat.items[0].dhuhr + "\n\nAsr - " +
                                 _salat.items[0].asr + "\n\nMaghrib - " + _salat.items[0].maghrib;         
                  NotifyPropertyChanged();
            }
        }

        public string SalatGenerated
        {
            get { return _salatGenerated; }
            set
            {
                if (value != null)
                {
                    _salatGenerated = value;
                    NotifyPropertyChanged();
                }
            }
        }

        private List<SalatItem> _salatTimes;

        public List<SalatItem> SalatTimes
        {
            get { return _salatTimes; }
            set
            {
                _salatTimes = value;
                NotifyPropertyChanged();
            }
        }

        /// <summary>
        /// The scheduler
        /// </summary>
        private readonly TaskScheduler _scheduler = TaskScheduler.FromCurrentSynchronizationContext();
        /// <summary>
        /// The device
        /// </summary>
        private IDevice _device;
        /// <summary>
        /// The cancel source
        /// </summary>
        private CancellationTokenSource _cancelSource;
        /// <summary>
        /// The get position command
        /// </summary>
        private Command _getPositionCommand;
        private IPhoneService _phoneService;
        private IGeolocator _geolocator;
        private string _salatGenerated;

        /// <summary>
        /// Gets the get position command.
        /// </summary>
        /// <value>The get position command.</value>
    /*    public Command GetPositionCommand
        {
            get
            {
                return _getPositionCommand ??
                    (_getPositionCommand = new Command(async () => await GetPosition(), () => Geolocator != null));
            }
        }*/
        /// <summary>
        /// Gets the open position URI.
        /// </summary>
        /// <value>The open position URI.</value>
        /* public ICommand OpenPositionUri
         {
             get
             {
                 return _openPositionUri ??
                     (_openPositionUri = new Command(
                         async () =>
                         {
                             var pos = await Geolocator.GetPositionAsync(5000);
                             var uri = Device.OnPlatform(
                                 pos.ToAppleMaps(),
                                 pos.ToUri(),
                                 pos.ToBingMaps());
                             Device.OpenUri(uri);
                         },
                         () => Geolocator != null));
             }
         }*/
        /// <summary>
        /// Gets the labs device.
        /// </summary>
        /// <value>The labs device.</value>
        private IDevice LabsDevice
        {
            get
            {
                return _device ?? (_device = Resolver.Resolve<IDevice>());
            }
        }
        /// <summary>
        /// Gets the phone service.
        /// </summary>
        /// <value>The phone service.</value>
        private IPhoneService PhoneService
        {
            get
            {
                return _phoneService ?? (_phoneService = DependencyService.Get<IPhoneService>());
            }
        }

        public SalahViewModel()
        {
            GetSalat();
            // GetPosition();
        }

        public async void GetSalat()
        {
            Dialogs.ShowLoading();
            try
            {
                Salat = await DependencyService.Get<ISalahServices>().GetSalatData();
            }
            catch(Exception e)
            {
                Insights.Report(e);
            }
            finally
            {
                Dialogs.HideLoading();
            }
        }
    }
}
